﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EnumBuilder
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            String namespaceString = ConfigurationManager.AppSettings["namespace"];
            String filepathString = ConfigurationManager.AppSettings["filepath"];

            if (!string.IsNullOrEmpty(namespaceString)) tbNameSpace.Text = namespaceString;

            if (!string.IsNullOrEmpty(filepathString)) tbFilePath.Text = filepathString;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            cfa.AppSettings.Settings["namespace"].Value = tbNameSpace.Text.Trim();
            cfa.AppSettings.Settings["filepath"].Value = tbFilePath.Text.Trim();
            cfa.Save();
            WXQDbContext<EnumBuilder.Dict> dbContext = new WXQDbContext<EnumBuilder.Dict>();
            var lt = dbContext.GetList("select * from dict", null);

            List<string> enumClassLt = lt.Where(d => d.DictType == "enum").GroupBy(d => d.GroupName).Select(c => c.Key).ToList();

            StringBuilder sb = new StringBuilder();

            #region 公共字典对象

            sb.Length = 0;
            sb.AppendLine("using System;");
            sb.AppendLine("using System.Collections.Generic;");
            sb.AppendLine("using System.Text;");
            sb.AppendLine("using System.ComponentModel;");
            sb.AppendLine("namespace " + tbNameSpace.Text.Trim());
            sb.AppendLine("{");
            sb.AppendLine("   #region Dict定义");
            sb.AppendLine("///<summary>");
            sb.AppendLine("/// Dict");
            sb.AppendLine("///</summary>");
            sb.AppendLine("public class Dict");
            sb.AppendLine("{");

            sb.AppendLine(" public string DictKey { get; set; } = string.Empty;");
            sb.AppendLine(" public string DictValue { get; set; } = string.Empty;");

            sb.AppendLine("  public string EnDecription { get; set; } = string.Empty;");

            sb.AppendLine(" public string CnDecription { get; set; } = string.Empty;");

            sb.AppendLine(" public int OrderSn { get; set; } = 0;");

            sb.AppendLine("}");
            sb.AppendLine("   #endregion Dict定义");
            sb.AppendLine("}");
            FIleHelper.WriteAllLines(tbFilePath.Text.Trim() + "/Dict.cs", sb.ToString());
            sb.Length = 0;

            #endregion 公共字典对象

            if (enumClassLt != null && enumClassLt.Count > 0)
            {
                foreach (string enumName in enumClassLt)
                {
                    sb.Length = 0;
                    sb.AppendLine("using System;");
                    sb.AppendLine("using System.Collections.Generic;");
                    sb.AppendLine("using System.Text;");
                    sb.AppendLine("using System.ComponentModel;");
                    sb.AppendLine("namespace " + tbNameSpace.Text.Trim());
                    sb.AppendLine("{");

                    #region 枚举定义

                    sb.AppendLine("   #region " + enumName + "枚举定义");
                    sb.AppendLine("///<summary>");
                    sb.AppendLine("/// " + enumName);
                    sb.AppendLine("///</summary>");
                    sb.AppendLine("public enum " + enumName);
                    sb.AppendLine("{");
                    List<EnumBuilder.Dict> enumItemLt = lt.Where(d => d.GroupName.Trim() == enumName.Trim()).OrderBy(d => d.OrderBy).ToList();
                    if (enumItemLt != null && enumItemLt.Count > 0)
                    {
                        for (int i = 0; i < enumItemLt.Count; i++)
                        {
                            sb.AppendLine("/// <summary>");
                            sb.AppendLine("/// " + (string.IsNullOrEmpty(enumItemLt[i].CnDescription) ? enumItemLt[i].EnDescription : enumItemLt[i].CnDescription));
                            sb.AppendLine("/// </summary>");
                            sb.AppendLine("" + enumItemLt[i].DictKey + "=" + enumItemLt[i].DictValue + (i != enumItemLt.Count - 1 ? "," : ""));
                        }
                    }
                    sb.AppendLine("}");
                    sb.AppendLine("   #endregion  ");

                    #endregion 枚举定义

                    sb.AppendLine("///<summary>");
                    sb.AppendLine("/// " + enumName + "扩展方法类");
                    sb.AppendLine("///</summary>");
                    sb.AppendLine(" public class " + enumName + "Extend");
                    sb.AppendLine("{");

                    sb.AppendLine("  private static Type GetType<T>()");
                    sb.AppendLine(" {");
                    sb.AppendLine("     return GetType(typeof(T));");
                    sb.AppendLine(" }");

                    sb.AppendLine("  private static Type GetType(Type type)");
                    sb.AppendLine("  {");
                    sb.AppendLine("    return Nullable.GetUnderlyingType(type) ?? type;");
                    sb.AppendLine("  }");

                    #region 值转枚举

                    sb.AppendLine("   #region 值转枚举");
                    sb.AppendLine("///<summary>");
                    sb.AppendLine("/// 值转枚举");
                    sb.AppendLine("///</summary>");
                    sb.AppendLine(" public static " + enumName + " Get" + enumName + "(object enumValue)");
                    sb.AppendLine("{");

                    sb.AppendLine("string value = enumValue == null ? string.Empty : enumValue.ToString().Trim();");
                    sb.AppendLine(" if (string.IsNullOrEmpty(value))");
                    sb.AppendLine(" {");
                    sb.AppendLine("   if (typeof(" + enumName + ").IsGenericType)");
                    sb.AppendLine("    {");
                    sb.AppendLine("        return default(" + enumName + ");");
                    sb.AppendLine("    }");
                    sb.AppendLine("   throw new ArgumentNullException(nameof(enumValue));");
                    sb.AppendLine(" }");

                    sb.AppendLine("  return (" + enumName + ")System.Enum.Parse(GetType<" + enumName + ">(), value, true);");

                    sb.AppendLine(" }");
                    sb.AppendLine("   #endregion  ");

                    #endregion 值转枚举

                    #region 获取名称

                    sb.AppendLine("   #region 获取名称");
                    sb.AppendLine("///<summary>");
                    sb.AppendLine("/// 获取名称");
                    sb.AppendLine("///</summary>");
                    sb.AppendLine(" public static string GetName(int enumValue)");
                    sb.AppendLine("{");
                    sb.AppendLine(" string result =string.Empty;");
                    sb.AppendLine(" switch (enumValue)");
                    sb.AppendLine("{");
                    if (enumItemLt != null && enumItemLt.Count > 0)
                    {
                        for (int i = 0; i < enumItemLt.Count; i++)
                        {
                            sb.AppendLine(" case " + enumItemLt[i].DictValue + ":");
                            sb.AppendLine("result = \"" + enumItemLt[i].DictKey + "\";");
                            sb.AppendLine("break;");
                            sb.AppendLine("");
                        }
                    }

                    sb.AppendLine(" default:");
                    sb.AppendLine(" break;");
                    sb.AppendLine(" }");
                    sb.AppendLine(" return result;");
                    sb.AppendLine(" }");
                    sb.AppendLine("   #endregion  ");

                    #endregion 获取名称

                    #region 获取名称

                    sb.AppendLine("   #region 获取名称");
                    sb.AppendLine("///<summary>");
                    sb.AppendLine("/// 获取名称");
                    sb.AppendLine("///</summary>");
                    sb.AppendLine(" public static string GetName(" + enumName + " enumObj)");
                    sb.AppendLine("{");
                    sb.AppendLine(" string result =string.Empty;");
                    sb.AppendLine(" switch (enumObj)");
                    sb.AppendLine("{");
                    if (enumItemLt != null && enumItemLt.Count > 0)
                    {
                        for (int i = 0; i < enumItemLt.Count; i++)
                        {
                            sb.AppendLine(" case " + enumName + "." + enumItemLt[i].DictKey + ":");
                            sb.AppendLine("result = \"" + enumItemLt[i].DictKey + "\";");
                            sb.AppendLine("break;");
                            sb.AppendLine("");
                        }
                    }

                    sb.AppendLine(" default:");
                    sb.AppendLine(" break;");
                    sb.AppendLine(" }");
                    sb.AppendLine(" return result;");
                    sb.AppendLine(" }");
                    sb.AppendLine("   #endregion  ");

                    #endregion 获取名称



                    #region 获取英文描述

                    sb.AppendLine("   #region 获取英文描述");
                    sb.AppendLine("///<summary>");
                    sb.AppendLine("/// 获取英文描述");
                    sb.AppendLine("///</summary>");
                    sb.AppendLine(" public static string GetDescriptionEn(" + enumName + " enumObj)");
                    sb.AppendLine("{");
                    sb.AppendLine(" string result =string.Empty;");
                    sb.AppendLine(" switch (enumObj)");
                    sb.AppendLine("{");
                    if (enumItemLt != null && enumItemLt.Count > 0)
                    {
                        for (int i = 0; i < enumItemLt.Count; i++)
                        {
                            sb.AppendLine(" case " + enumName + "." + enumItemLt[i].DictKey + ":");
                            sb.AppendLine("result = \"" + enumItemLt[i].EnDescription + "\";");
                            sb.AppendLine("break;");
                            sb.AppendLine("");
                        }
                    }

                    sb.AppendLine(" default:");
                    sb.AppendLine(" break;");
                    sb.AppendLine(" }");
                    sb.AppendLine(" return result;");
                    sb.AppendLine(" }");
                    sb.AppendLine("   #endregion  ");

                    #endregion 获取英文描述



                    #region 获取中文描述

                    sb.AppendLine("   #region 获取中文描述");
                    sb.AppendLine("///<summary>");
                    sb.AppendLine("/// 获取中文描述");
                    sb.AppendLine("///</summary>");
                    sb.AppendLine(" public static string GetDescriptionCn(" + enumName + " enumObj)");
                    sb.AppendLine("{");
                    sb.AppendLine(" string result =string.Empty;");
                    sb.AppendLine(" switch (enumObj)");
                    sb.AppendLine("{");
                    if (enumItemLt != null && enumItemLt.Count > 0)
                    {
                        for (int i = 0; i < enumItemLt.Count; i++)
                        {
                            sb.AppendLine(" case " + enumName + "." + enumItemLt[i].DictKey + ":");
                            sb.AppendLine("result = \"" + enumItemLt[i].CnDescription + "\";");
                            sb.AppendLine("break;");
                            sb.AppendLine("");
                        }
                    }

                    sb.AppendLine(" default:");
                    sb.AppendLine(" break;");
                    sb.AppendLine(" }");
                    sb.AppendLine(" return result;");
                    sb.AppendLine(" }");
                    sb.AppendLine("   #endregion  ");

                    #endregion 获取中文描述



                    #region 获取枚举列表
                    sb.AppendLine(" private static List<" + enumName + "> EnumLt ;");
                    sb.AppendLine("   #region 获取列表");
                    sb.AppendLine("///<summary>");
                    sb.AppendLine("/// 获取列表");
                    sb.AppendLine("///</summary>");
                    sb.AppendLine(" public static List<" + enumName + "> GetList()");
                    sb.AppendLine("{");

                    sb.AppendLine(" if (EnumLt!=null&& EnumLt.Count>0)");
                    sb.AppendLine("{");
                    sb.AppendLine("return EnumLt;");
                    sb.AppendLine(" }");
                    sb.AppendLine("else");
                    sb.AppendLine("{");

                  

                    sb.AppendLine(" EnumLt =new List<" + enumName + ">();");

                    if (enumItemLt != null && enumItemLt.Count > 0)
                    {
                        for (int i = 0; i < enumItemLt.Count; i++)
                        {
                            sb.AppendLine(" EnumLt.Add( " + enumName+"." + enumItemLt[i].DictKey + ");   ");
                        }
                    }
                    sb.AppendLine(" }");

                    sb.AppendLine(" return EnumLt;");
                    sb.AppendLine(" }");
                    sb.AppendLine("   #endregion  ");

                    #endregion 获取枚举列表

                    #region 获取通用集合

                    sb.AppendLine(" private static List<Dict> dictLt ;");
                    sb.AppendLine("   #region 获取通用集合");
                    sb.AppendLine("///<summary>");
                    sb.AppendLine("/// 获取通用集合");
                    sb.AppendLine("///</summary>");
                    sb.AppendLine(" public static List<Dict> GetDictList()");
                    sb.AppendLine("{");

                    sb.AppendLine(" if (EnumLt!=null&& EnumLt.Count>0)");
                    sb.AppendLine("{");
                    sb.AppendLine("return dictLt;");
                    sb.AppendLine(" }");
                    sb.AppendLine("else");
                    sb.AppendLine("{");


                    sb.AppendLine("  dictLt =new List<Dict>();");
         
                    if (enumItemLt != null && enumItemLt.Count > 0)
                    {
              
                        for (int i = 0; i < enumItemLt.Count; i++)
                        {
                            sb.AppendLine(" Dict m"+i+"=new Dict();");
                            sb.AppendLine(" m" + i + ".DictKey=\"" + enumItemLt[i].DictKey + "\";");
                            sb.AppendLine(" m" + i + ".DictValue=\"" + enumItemLt[i].DictValue + "\";");
                            sb.AppendLine(" m" + i + ".EnDecription=\"" + enumItemLt[i].EnDescription + "\";");
                            sb.AppendLine(" m" + i + ".CnDecription=\"" + enumItemLt[i].CnDescription + "\";");
                            sb.AppendLine(" m" + i + ".OrderSn=" + enumItemLt[i].OrderBy+";");
                            sb.AppendLine(" dictLt.Add(m" + i + ");");
                        }
                 

                    }
                    sb.AppendLine(" }");

                    sb.AppendLine(" return dictLt;");



                    sb.AppendLine(" }");
                    sb.AppendLine("   #endregion  ");

                    #endregion 获取通用集合

                    sb.AppendLine(" }");

                    sb.AppendLine("}");

                    FIleHelper.WriteAllLines(tbFilePath.Text.Trim() + "/" + enumName + ".cs", sb.ToString());
                }
            }
        }
    }
}